﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

namespace GreatB2C.Web.Controls
{
    public partial class Region : System.Web.UI.UserControl
    {
        protected void Page_Load( object sender , EventArgs e )
        {
        }

        #region 属性
        /// <summary>
        /// 国家/地区
        /// </summary>
        public string Province
        {
            get { return ddlProvince.SelectedValue.ToString( ); }
            set { ddlProvince.SelectedValue = value; ddlProvince_SelectedIndexChanged( null , null ); }
        }

        /// <summary>
        /// 省/市/自治区
        /// </summary>
        public string City
        {
            get { return ddlCity.SelectedValue.ToString( ); }
            set { ddlCity.SelectedValue = value; ddlCity_SelectedIndexChanged( null , null ); }
        }

        /// <summary>
        /// 市/县/区/旗
        /// </summary>
        public string Area
        {
            get { return ddlArea.SelectedValue.ToString( ); }
            set { ddlArea.SelectedValue = value; }
        }

        #endregion

        #region 事件
        protected void ddlProvince_SelectedIndexChanged( object sender , EventArgs e )
        {
            if ( ddlProvince.SelectedValue.Trim( ) != "" )
            {
                Business.City city = new Business.City( );
                DataTable dtCity = city.ListCityByProvince( ddlProvince.SelectedValue.Trim( ) );
                if ( dtCity != null && dtCity.Rows.Count > 0 )
                {
                    ddlCity.DataSource = dtCity;
                    ddlCity.DataTextField = "name";
                    ddlCity.DataValueField = "code";
                    ddlCity.DataBind( );
                }

                ddlCity.Items.Insert( 0 , new ListItem( "-请选择-" , "" ) );
            }
        }

        protected void ddlCity_SelectedIndexChanged( object sender , EventArgs e )
        {
            if ( ddlCity.SelectedValue.Trim( ) != "" )
            {
                Business.Area area = new Business.Area( );
                DataTable dtArea = area.ListAreaByCity( ddlCity.SelectedValue.Trim( ) );
                if ( dtArea != null && dtArea.Rows.Count > 0 )
                {
                    ddlArea.DataSource = dtArea;
                    ddlArea.DataTextField = "name";
                    ddlArea.DataValueField = "code";
                    ddlArea.DataBind( );
                }

                ddlArea.Items.Insert( 0 , new ListItem( "-请选择-" , "" ) );
            }
        }

        /// <summary>
        /// 数据初始化
        /// </summary>
        public void DataInitial( )
        {
            if ( ddlProvince.Items.Count > 0 )
            {
                // 已经加载了
            }
            else
            {
                // 没有加载
                Business.Province prov = new Business.Province( );
                DataTable dtProvince = prov.ListProvince( );
                if ( dtProvince != null && dtProvince.Rows.Count > 0 )
                {
                    ddlProvince.DataSource = dtProvince;
                    ddlProvince.DataTextField = "name";
                    ddlProvince.DataValueField = "code";
                    ddlProvince.DataBind( );
                }

                ddlProvince.Items.Insert( 0 , new ListItem( "-请选择-" , "" ) );
                ddlCity.Items.Insert( 0 , new ListItem( "-请选择-" , "" ) );
                ddlArea.Items.Insert( 0 , new ListItem( "-请选择-" , "" ) );
            }
        }
        #endregion
    }
}